package org.chench.extra.logsift.service.impl;

import org.chench.extra.logsift.service.LogHandler;
import org.springframework.stereotype.Service;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

/**
 * 在控制台打印日志信息
 * @author chench
 * @date 2025.05.17
 */
@Service
public class ConsoleHandlerImpl implements LogHandler {
    @Override
    public void handleLog(String logAppName, Path filePath, Map<String, Object> logData, String line) {
        if (logData == null) {
            // 日志行模式匹配失败，属于异常信息
            logData = new HashMap<>();
            logData.put("message", line);
        } else if (logData.isEmpty()) {
            // 处理级别日志
            logData.put("message", line);
        }
        System.out.println("-----------------------日志处理------------------------");
        System.out.println("捕获错误日志信息:");
        System.out.println("日志应用名：" + logAppName);
        System.out.println("文件名：" + filePath.getFileName());
        System.out.println("时间: " + logData.getOrDefault("timestamp", ""));
        System.out.println("级别: " + logData.getOrDefault("level", ""));
        System.out.println("日志器: " + logData.getOrDefault("logger", ""));
        System.out.println("内容: " + logData.get("message"));
        System.out.println("------------------------------------------------------");
    }
}